package com.probiz.estoremf.content.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.catalog.model.ContentCategorySearchCriteria;
import com.probiz.estore.catalog.service.ContentCategoryManager;
import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.content.model.ContentSearchCriteria;
import com.probiz.estore.content.service.ContentManager;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;

public class HelpCenterSearchFrontController extends	GenericStoreFrontController<Content> {
	/**
	 * 内容目录最大搜索数
	 */
	public final static Integer MAX_CONTENT_CATEGORY_SEARCH_COUNT    =  Integer.valueOf(1000);
	
	private ContentManager	contentManager;
	private ContentCategoryManager	contentCategoryManager;
	

	public void setContentManager(ContentManager contentManager) {
		this.contentManager = contentManager;
	}


	public void setContentCategoryManager(ContentCategoryManager contentCategoryManager) {
		this.contentCategoryManager = contentCategoryManager;
	}

	protected void initController() throws Exception {
		mgr = contentManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ContentCategory contentCategory = contentCategoryManager.getCategoryByCode("helpCenter");
		//1. 特别注意先查询搜索帮助中心目录
		SearchCriteria searchCriteria4Category = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
		searchCriteria4Category.changePaging(1, MAX_CONTENT_CATEGORY_SEARCH_COUNT);
		ContentCategorySearchCriteria contentCategorySearchCriteria = new ContentCategorySearchCriteria();
		bindOnly(request, contentCategorySearchCriteria, "contentCategorySearchCriteria");
		contentCategorySearchCriteria.setCategoryPath(contentCategory.getCategoryPath());
		contentCategorySearchCriteria.setStatus(ContentCategory.STATUS_ACTIVE);
		List<ContentCategory> contentCategoryList = contentCategoryManager.searchByCriteria(contentCategorySearchCriteria,	searchCriteria4Category);
		

		//2. 再搜索文章，因为这时候的返回的页面SC是文章的。
		SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
		searchCriteria.changePaging(searchCriteria.getPageNo(), 20);
		// ------start:构造前台查询条件------
		ContentSearchCriteria contentSearchCriteria = new ContentSearchCriteria();
		
		bindOnly(request, contentSearchCriteria, "contentSearchCriteria");
		contentSearchCriteria.setStatus(Content.STATUS_PUBLISHED);
		contentSearchCriteria.setOrderby((short)1);
		contentSearchCriteria.setCategoryPath(contentCategory.getCategoryPath());
		// ------end:构造前台查询条件------
		List<Content> contentList = contentManager.searchByCriteriaForController(contentSearchCriteria,	searchCriteria);
		
		

		ModelAndView mv = new ModelAndView("content/helpCenter/defaultHelpCenterSearchList");
		mv.addObject("contentList", contentList);
		mv.addObject("contentCategoryList", contentCategoryList);
		mv.addObject("contentSearchCriteria", contentSearchCriteria);

		return mv;
	}



	
}
